package com.com.zs21cp.wordcountDemo;


import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;



public class flink_wc_Stream_01 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        DataStreamSource<String> DS = env.readTextFile("F:\\java\\test_demo\\flink\\input.txt");

        DataStreamSource<String> DS = env.socketTextStream("localhost", 10086);
        SingleOutputStreamOperator<Tuple2<String, Long>> map = DS.flatMap((String s, Collector<Tuple2<String, Long>> out) -> {
            String[] s1 = s.split(" ");
            for (String s2 : s1) {
                out.collect(Tuple2.of(s2, 1L));
            }
    }).returns(Types.TUPLE(Types.STRING, Types.LONG));
//        KeyedStream<Tuple2<String, Integer>, String> sum = map.keyBy(data -> data.f0);
        KeyedStream<Tuple2<String, Long>, Object> tuple2StringKeyedStream = map.keyBy(stringIntegerTuple2 -> stringIntegerTuple2.f0);
        tuple2StringKeyedStream.sum(1).print();


        env.execute();




    }
}
